/**Created by the LayaAirIDE*/
module view.dlg{
	export class Prize extends ui.dlg.PrizeUI{
		private roleAni:Laya.Animation;
		private id:number ;
		private darwLevel:number;
		private baoxiang:boolean;
		private scaleDelta: number = 0;
	
		constructor(){
			
			super();
			this.onOpened();
			this.zhongzi.visible = false;
			this.on(Laya.Event.CLICK,this,this.onTreasureBox);
			this.baoxiang = false;
			this.dazzle0.visible = false;
			this.dazzle1.visible = false;
			this.num.visible = false;
		}
		onTreasureBox():void{
			this.roleAni.visible = false;
			this.zhongzi.visible = false
		}
		setRoleAni():void{

		}
		
		//var{} = setRoleAni();
		darwShow(id:number,darwLevel:number , multiple = true):void{

			this.id = id;
			this.darwLevel = darwLevel;
			let prizeMultiple = 1;
			if(multiple) prizeMultiple = view.Farm.luckDarw.getPrizeMultiple();
			if (darwLevel == 1 || darwLevel == 2 || darwLevel == 3 || darwLevel == 8){
				this.roleAni = new Laya.Animation();
				var url = "res/atlas/baoxiang_" + darwLevel + ".atlas";
				// this.roleAni.x = this.zhongzi.x - 140;
				// this.roleAni.y = this.zhongzi.y - 190;			
				//Laya.stage.addChild(this.roleAni);
				this.roleAni.zOrder = 30;
				this.zhongzi.zOrder = 31;
				if (darwLevel == 1 || darwLevel == 2 || darwLevel == 3){
					this.roleAni.scaleX = 1;
					this.roleAni.scaleY = 1;
					this.roleAni.x += 100;
				}
				else{
					this.roleAni.x += 60;
					this.roleAni.y += 180;
				}
				this.roleAni.loadAtlas(url,Laya.Handler.create(this,this.onLoaded,[prizeMultiple]));			
			}
			else
			{				
				this.showPrize(prizeMultiple);
			}
			
		}
		onLoaded(prizeMultiple):void{
			//添加到舞台
			//this.roleAni.autoPlay = true;
			//this.roleAni.index = 1;
			this.roleAni.play(0,false);
			this.addChild(this.roleAni);
			if (this.darwLevel == 1 || this.darwLevel == 2 || this.darwLevel == 3){
				this.roleAni.interval = 142; // 设置播放间隔（单位：毫秒
			}else if(this.darwLevel == 8){
				this.roleAni.interval = 115;
			}
			//播放动画
			
			
			//this.roleAni.property = Laya.Event.COMPLETE;	
			this.baoxiang = true;
			Laya.timer.once(1000,this,this.showPrize,[prizeMultiple]);
		}

		showPrize(prizeMultiple):void{
			
			this.zhongzi.skin = "item/" + Cfg.LuckDarwCfg[this.id]["pictureName"] + ".png";
			this.num.text = Cfg.LuckDarwCfg[this.id]["name"]+ "x" + Cfg.LuckDarwCfg[this.id]["num"]*prizeMultiple;
			this.zhongzi.visible = true;
			this.zhongzi.scale(2,2);
			if (this.baoxiang) {
				Laya.Tween.to(this.zhongzi,{y:this.zhongzi.y - 200,scaleX:2,scaleY:2},800,Laya.Ease.cubicOut);
				Laya.timer.once(800,this,this.fallPrize );
				Laya.timer.once(1600,this,this.showDazzle );
			}
		}
		fallPrize():void{
			Laya.Tween.to(this.zhongzi,{y:this.zhongzi.y + 200,scaleX:2,scaleY:2},800,Laya.Ease.cubicOut);
		}
		showDazzle():void{
			this.roleAni.visible = false;
			//this.roleAni.destroy();
			this.dazzle0.x = this.zhongzi.x;
			this.dazzle0.y = this.zhongzi.y;
			this.dazzle1.x = this.zhongzi.x;
			this.dazzle1.y = this.zhongzi.y;
			this.dazzle1.scaleX = 1.5;
			this.dazzle1.scaleY = 1.5;
			this.dazzle0.scaleX = 1.5;
			this.dazzle0.scaleY = 1.5;
			this.dazzle0.visible = false;
			this.dazzle1.visible = true;
			this.num.visible = true;
			Laya.timer.frameLoop(1, this, this.animate);
		}

		private animate(e: Event): void {
		 	this.dazzle0.rotation += 20 ;
			this.dazzle1.rotation += 1.5 ;
			this.scaleDelta += 0.02;
			var scaleValue: number = Math.sin(this.scaleDelta);
			this.dazzle0.scale(1, 1);
		}
	}
}